﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Linq;
namespace WellDone
{
    public partial class CopyMenuItemDialog : ChildWindow
    {
        public CopyMenuItemDialog()
        {
            InitializeComponent();
        }

        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Globals.showWaitCursor(true);
                btnOK.IsEnabled = false;
                btnCancel.IsEnabled = false;
                if (tbMenuItemName.Text.Trim().Length > 0)
                {
                    //Checked for duplicate menu item                    
                    bool isExist = Globals.menuItems.cmbMenuItems.Items.Contains(tbMenuItemName.Text.Trim());
                    if (isExist)
                    {
                        MessageBox.Show("menu item already exists.", "Duplicate Menu Item", MessageBoxButton.OK);                        
                    }
                    else
                    {
                        //getting menu item record based on Id.
                        ServiceReferenceOpenRPOS.MenuItem CopyMenuItemRecord = Globals.db.MenuItemRecords.Where(m=>m.col_ID==Globals.menuItemID).FirstOrDefault();
                        if (CopyMenuItemRecord != null) 
                        { 
                            var CopyMenuItemOptionGroupRecords = Globals.db.MenuItemOptionGroupRecords.Where(og => og.col_MenuItemID == Globals.menuItemID);
                            
                            ServiceReferenceOpenRPOS.MenuItem mi = new ServiceReferenceOpenRPOS.MenuItem();
                            ServiceReferenceOpenRPOS.MenuItemOptionGroup miog = new ServiceReferenceOpenRPOS.MenuItemOptionGroup();                        

                            mi.col_FullName = tbMenuItemName.Text.Trim();
                            mi.bFilterFullName = true;
                            mi.col_Active = CopyMenuItemRecord.col_Active;
                            mi.bFilterActive = true;
                            mi.col_Pizza = (cbPizaa.IsChecked == true);
                            mi.bFilterPizza = true;
                            mi.col_SpecialtyPizza = (cbSpecialityPizaa.IsChecked == true);
                            mi.bFilterSpecialtyPizza = true;
                            mi.col_DoubleOptions = (cbDoubleOption.IsChecked == true);
                            mi.bFilterDoubleOptions = true;
                            if (cbIncludeOptions.IsChecked == true)
                            {
                                mi.col_IncludeOptions = 1;
                                mi.bFilterIncludeOptions = true;
                            }
                            else
                            {
                                mi.col_IncludeOptions = 0;
                                mi.bFilterIncludeOptions = true;
                            }
                            mi.col_Description = CopyMenuItemRecord.col_Description;
                            mi.bFilterDescription = true;
                            mi.col_Enabled = CopyMenuItemRecord.col_Enabled;
                            mi.bFilterEnabled = true;
                            mi.col_MenuCategoryID = CopyMenuItemRecord.col_MenuCategoryID;
                            mi.bFilterMenuCategoryID = true;
                            mi.col_Price = CopyMenuItemRecord.col_Price;
                            mi.bFilterPrice = true;
                            mi.col_RecipeSummaryID = CopyMenuItemRecord.col_RecipeSummaryID;
                            mi.bFilterRecipeSummaryID = true;
                            mi.col_ShortName = CopyMenuItemRecord.col_ShortName;
                            mi.bFilterShortName = true;
                            int CopyMenuID = await Globals.db.AddMenuItemObject(mi);

                            foreach (ServiceReferenceOpenRPOS.MenuItemOptionGroup mio in CopyMenuItemOptionGroupRecords)
                            {
                                try
                                {
                                    miog.col_MenuItemID = CopyMenuID;
                                    miog.bFilterMenuItemID = true;
                                    miog.col_OptionGroupID = mio.col_OptionGroupID;
                                    miog.bFilterOptionGroupID = true;
                                    miog.col_Precedence = mio.col_Precedence;
                                    miog.bFilterPrecedence = true;
                                    await Globals.db.AddMenuItemOptionGroupObject(miog);
                                }
                                catch (Exception) { }
                            }
                            Globals.menuItems.initMenuItems(true);
                            this.DialogResult = true;
                        }                        
                    }
                }
            }
            catch (Exception) { }
            finally
            {
                // //Enable buttons
                btnOK.IsEnabled = true;
                btnCancel.IsEnabled = true;
                Globals.showWaitCursor(false);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void tbMenuItemName_KeyDown(object sender, KeyEventArgs e)
        {
            // Call the OK button handlers if "Enter" is pressed
            if (e.Key == Key.Enter) btnOK_Click(sender, e);
        }
    }
}